package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.question.domain.Feedback;
import com.wuwei.elearning.question.domain.Question;
import com.wuwei.elearning.question.mapper.FeedbackMapper;
import com.wuwei.elearning.question.mapper.QuestionMapper;
import com.wuwei.elearning.setting.domain.User;
import com.wuwei.elearning.setting.mapper.UserMapper;
import com.wuwei.elearning.utils.result.Result;
import com.wuwei.elearning.utils.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-02-01
 */
@RestController
@RequestMapping("/elearning/feedback")
@CrossOrigin
public class FeedbackController {

    @Resource
    private FeedbackMapper feedbackMapper;

    @Resource
    private HttpServletRequest request;

    @Resource
    private UserMapper userMapper;

    @Resource
    private QuestionMapper questionMapper;

    /**
     * 用户点击反馈错题，即可触发此方法
     * @param questionId
     * @return
     */
    @PutMapping("insert/{questionId}")
    public Result<String> insert(@PathVariable("questionId") Long questionId){
        Question question = questionMapper.selectById(questionId);
        //获取用户名
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        LambdaQueryWrapper<User> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(User::getAccount,account);
        String name = userMapper.selectOne(queryWrapper2).getName();
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Feedback::getQuestionId,questionId);
        Feedback feedback = feedbackMapper.selectOne(wrapper);
        //如果Feedback表中查询不出记录，表示没有人反馈过，新建一条记录
        if (feedback==null) {
            feedback = new Feedback();
            feedback.setQuestionId(questionId);
            feedback.setCreatedTime(LocalDateTime.now());
            feedback.setCreatedName(name);
            feedback.setCreatedBy(account);
            feedback.setTimes(1);
            feedback.setQuestionName(question.getQuestion());
            feedbackMapper.insert(feedback);
        } else {
            //否则在原来的次数的基础上加1
            feedback.setTimes(feedback.getTimes()+1);
            feedback.setQuestionName(question.getQuestion());
            feedbackMapper.updateById(feedback);
        }

        return Result.ok("反馈成功！");
    }

    @GetMapping("getAll")
    public Result<List<Feedback>> getAll(){

        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();

        List<Feedback> feedbacks = feedbackMapper.selectList(wrapper);

        return Result.ok(feedbacks);
    }

    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable("id")Long id){
        feedbackMapper.deleteById(id);

        return Result.ok("删除成功！");
    }

}

